package com.dove.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dove.common.Constants;
import com.dove.common.Result;
import com.dove.common.exception.ServiceException;
import com.dove.entity.Course;
import com.dove.mapper.CourseMapper;
import com.dove.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dove
 * @since 2022-03-31
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public IPage<Course> myFindPage(Page<Course> page, String name) {
        return courseMapper.myFindPage(page, name);
    }

    @Override
    public List<Course> findCourseByTeacherId(Integer id) {
        QueryWrapper<Course> qw = new QueryWrapper<>();
        qw.eq("teacher_id", id);
        List<Course> list = courseMapper.selectList(qw);
        return list;
    }

    @Transactional
    @Override
    public Boolean chooseCourse(Integer studentId,Integer courseId) {
        Boolean f1 = courseMapper.deleteStudentCourse(studentId, courseId);
        Boolean f2 = courseMapper.insertStudentCourse(studentId, courseId);
        if (f1&&f2) {
            return true;
        }
        return false;
    }
}
